# Contributor: Rafal Brzegowy <3rav@hotmail.com>

_realname=med
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.3.1
pkgrel=2
pkgdesc="MED is the Data Echange Module based on the HDF library (mingw-w64)"
arch=('any')
url="https://www.salome-platform.org/downloads/current-version"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('!strip' '!buildflags' 'staticlibs')
source=("http://files.salome-platform.org/Salome/other/med-${pkgver}.tar.gz")
sha256sums=('DD631EF813838BC7413FF0DD6461D7A0D725BCFABABDF772ECE67610A8D22588')

build() {
  # Build static library
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DMEDFILE_BUILD_SHARED_LIBS=OFF \
    -DMEDFILE_BUILD_STATIC_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${srcdir}/${_realname}-${pkgver}_SRC"
  make
  
  # Build shared library
  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-shared-${MINGW_CHOST} && cd ${srcdir}/build-shared-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DMEDFILE_BUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${srcdir}/${_realname}-${pkgver}_SRC"
  make
}

package() {
  cd ${srcdir}/build-static-${MINGW_CHOST}/src
  make DESTDIR=${pkgdir} install
   
  cd ${srcdir}/build-static-${MINGW_CHOST}/include
  make DESTDIR=${pkgdir} install
  
  cd ${srcdir}/build-shared-${MINGW_CHOST}/src
  make DESTDIR=${pkgdir} install
   
  cd ${srcdir}/build-shared-${MINGW_CHOST}/include
  make DESTDIR=${pkgdir} install
  
  install -Dm644 ${srcdir}/${_realname}-${pkgver}_SRC/COPYING.LESSER \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER

  install -Dm644 ${srcdir}/${_realname}-${pkgver}_SRC/COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
